﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Windows.Forms;

namespace Marmat.Forms
{
    public class GDI
    {
        /// <summary>
        /// 剪切图片底框
        /// </summary>
        /// <param name="Image"></param>
        /// <param name="Size"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static Image CutBottomFrame(Image Image, Size Size, int x)
        {
            Rectangle Rectangle;
            using (var bmp = new Bitmap(Size.Width, Size.Height))
            {
                using (var G_Bmp = Graphics.FromImage(bmp))
                {
                    // 四个角
                    Rectangle = new Rectangle(0, 0, x, x);
                    G_Bmp.DrawImage(Image, Rectangle, Rectangle, GraphicsUnit.Pixel); // 左上
                    G_Bmp.DrawImage(Image, new Rectangle(0, Size.Height - x, x, x), new Rectangle(0, Image.Height - x, x, x), GraphicsUnit.Pixel); // 左下
                    G_Bmp.DrawImage(Image, new Rectangle(Size.Width - x, 0, x, x), new Rectangle(Image.Width - x, 0, x, x), GraphicsUnit.Pixel); // 右上
                    G_Bmp.DrawImage(Image, new Rectangle(Size.Width - x, Size.Height - x, x, x), new Rectangle(Image.Width - x, Image.Height - x, x, x), GraphicsUnit.Pixel); // 右下
                                                                                                                                                                              // 四边
                    G_Bmp.DrawImage(Image, new Rectangle(0, x, x, Size.Height - x * 2), new Rectangle(0, x, x, Image.Height - x * 2), GraphicsUnit.Pixel); // 左边
                    G_Bmp.DrawImage(Image, new Rectangle(x, 0, Size.Width - x * 2, x), new Rectangle(x, 0, Image.Width - x * 2, x), GraphicsUnit.Pixel); // 上边
                    G_Bmp.DrawImage(Image, new Rectangle(x, Size.Height - x, Size.Width - x * 2, x), new Rectangle(x, Image.Height - x, Image.Width - x * 2, x), GraphicsUnit.Pixel); // 下边
                    G_Bmp.DrawImage(Image, new Rectangle(Size.Width - x, x, x, Size.Height - x * 2), new Rectangle(Image.Width - x, x, x, Image.Height - x * 2), GraphicsUnit.Pixel); // 右边
                    G_Bmp.DrawImage(Image, new Rectangle(Size.Width - x, x, x, Size.Height - x * 2), new Rectangle(Image.Width - x, x, x, Image.Height - x * 2), GraphicsUnit.Pixel); // 中间
                    G_Bmp.DrawImage(Image, new Rectangle(x, x, Size.Width - x * 2, Size.Height - x * 2), new Rectangle(x, x, Image.Width - x * 2, Image.Height - x * 2), GraphicsUnit.Pixel); // 中间
                    return (Image)bmp.Clone();
                    G_Bmp.Dispose();
                    bmp.Dispose();
                }
            }
        }

        public static void DrawText(Graphics e, string txt, Font Font, Rectangle Rectangle, Color ForeColor, Color BackColor, ContentAlignment Alignment)
        {
            var Flags = TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;
            switch (Alignment)
            {
                case ContentAlignment.TopLeft:
                    {
                        Flags = Flags;
                        break;
                    }

                case ContentAlignment.TopCenter:
                    {
                        Flags = Flags | TextFormatFlags.HorizontalCenter;
                        break;
                    }

                case ContentAlignment.TopRight:
                    {
                        Flags = Flags | TextFormatFlags.Right;
                        break;
                    }

                case ContentAlignment.MiddleLeft:
                    {
                        Flags = Flags | TextFormatFlags.VerticalCenter;
                        break;
                    }

                case ContentAlignment.MiddleCenter:
                    {
                        Flags = Flags | TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
                        break;
                    }

                case ContentAlignment.MiddleRight:
                    {
                        Flags = Flags | TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
                        break;
                    }

                case ContentAlignment.BottomLeft:
                    {
                        Flags = Flags | TextFormatFlags.Bottom;
                        break;
                    }

                case ContentAlignment.BottomCenter:
                    {
                        Flags = Flags | TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
                        break;
                    }

                case ContentAlignment.BottomRight:
                    {
                        Flags = Flags | TextFormatFlags.Bottom | TextFormatFlags.Right;
                        break;
                    }
            }

            TextRenderer.DrawText(e, txt, Font, Rectangle, ForeColor, BackColor, Flags);
        }

        /// <summary>
        /// 获取圆角矩形路径
        /// </summary>
        /// <param name="Rect"></param>
        /// <param name="Round"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static GraphicsPath GetRoundRectangle(Rectangle Rect, int Round)
        {
            return GetRoundRectangle(Rect, new ArcRadius(Round, Round, Round, Round));
        }

        /// <summary>
        /// 获取圆角矩形路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="Round"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static GraphicsPath GetRoundRectangle(Rectangle rect, ArcRadius Round)
        {
            var Path = new GraphicsPath();
            if (rect.Width == 0 || rect.Height == 0)
            {
                return Path;
            }
            else if (Round.All == 0)
            {
                Path.AddRectangle(rect);
                return Path;
            }

            if (Round.LeftTop > 0)
            {
                Path.AddArc(rect.Left, rect.Top, Round.LeftTop * 2, Round.LeftTop * 2, 180f, 90f);
            }

            Path.AddLine(new Point(rect.Left + Round.LeftTop * 2, rect.Top), new Point(rect.Right - Round.RightTop * 2, rect.Top));
            if (Round.RightTop > 0)
            {
                Path.AddArc(rect.Right - Round.RightTop * 2 - 1, rect.Top, Round.RightTop * 2, Round.RightTop * 2, -90, 90f);
            }

            Path.AddLine(new Point(rect.Right, rect.Top + Round.RightTop * 2), new Point(rect.Right, rect.Bottom - Round.RightBottom * 2));
            if (Round.RightBottom > 0)
            {
                Path.AddArc(rect.Right - Round.RightBottom * 2 - 1, rect.Bottom - Round.RightBottom * 2 - 1, Round.RightBottom * 2, Round.RightBottom * 2, 0f, 90f);
            }

            Path.AddLine(new Point(rect.Right - Round.RightBottom * 2, rect.Bottom), new Point(rect.Left + Round.LeftBottom * 2, rect.Bottom));
            if (Round.LeftBottom > 0)
            {
                Path.AddArc(rect.Left, rect.Bottom - Round.LeftBottom * 2 - 1, Round.LeftBottom * 2, Round.LeftBottom * 2, 90f, 90f);
            }

            Path.AddLine(new Point(rect.Left, rect.Bottom - Round.LeftBottom * 2), new Point(rect.Left, rect.Top + Round.LeftTop * 2));
            Path.CloseFigure();
            return Path;
        }

        /// <summary> 
        /// 获取指定图片中的平均颜色 
        /// </summary>
        /// <param name="Bitmap"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static Color GetBitmapAverageColor(Bitmap Bitmap)
        {
            var bmpData = new BitmapData();
            bmpData = Bitmap.LockBits(new Rectangle(0, 0, Bitmap.Width - 1, Bitmap.Height - 1), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            long R = 0L;
            long G = 0L;
            long B = 0L;
            long N = 0L;
            var Bts = new byte[bmpData.Stride * bmpData.Height + 1];
            System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, Bts, 0, Bts.Length);
            for (int I = 0, loopTo = Bts.Length - 4; I <= loopTo; I += 4)
            {
                try
                {
                    R += Bts[I + 2];
                    G += Bts[I + 1];
                    B += Bts[I];
                    N += 1L;
                }
                catch
                {
                }
            }

            Bitmap.UnlockBits(bmpData);
            try
            {
                R /= N;
                G /= N;
                B /= N;
                if (R < 0L)
                    R = 0L;
                if (R > 255L)
                    R = 255L;
                if (G < 0L)
                    G = 0L;
                if (G > 255L)
                    G = 255L;
                if (B < 0L)
                    B = 0L;
                if (B > 255L)
                    B = 255L;
            }
            catch
            {
            }

            return Color.FromArgb((int)R, (int)G, (int)B);
        }

        /// <summary>
        /// 绘制辉光文字
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="Font"></param>
        /// <param name="ForeColor"></param>
        /// <param name="BackColor"></param>
        /// <param name="BlurConsideration"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static Image ImageLightEffect(string txt, Font Font, Color ForeColor, Color BackColor, int BlurConsideration)
        {
            Bitmap Var_Bitmap = null;
            using (var g = Graphics.FromHwnd(IntPtr.Zero))
            {
                var size = g.MeasureString(txt, Font);
                using (var var_bmp = new Bitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height)))
                {
                    using (var var_g_bmp = Graphics.FromImage(var_bmp))
                    {
                        using (var var_BrushBack = new SolidBrush(Color.FromArgb(16, BackColor)))
                        {
                            using (var var_ForeColor = new SolidBrush(ForeColor))
                            {
                                var_g_bmp.SmoothingMode = SmoothingMode.HighQuality;
                                var_g_bmp.InterpolationMode = InterpolationMode.HighQualityBilinear;
                                var_g_bmp.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                                var_g_bmp.DrawString(txt, Font, var_BrushBack, 0f, 0f);
                                Var_Bitmap = new Bitmap(var_bmp.Width + BlurConsideration, var_bmp.Height + BlurConsideration);
                                using (var var_g_bitmap = Graphics.FromImage(Var_Bitmap))
                                {
                                    var_g_bitmap.SmoothingMode = SmoothingMode.HighQuality;
                                    var_g_bitmap.InterpolationMode = InterpolationMode.HighQualityBilinear;
                                    var_g_bitmap.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                                    for (int x = 0, loopTo = BlurConsideration; x <= loopTo; x++)
                                    {
                                        for (int y = 0, loopTo1 = BlurConsideration; y <= loopTo1; y++)
                                            var_g_bitmap.DrawImageUnscaled(var_bmp, x, y);
                                    }

                                    var_g_bitmap.DrawString(txt, Font, var_ForeColor, (float)(BlurConsideration / 2d), (float)(BlurConsideration / 2d));
                                }
                            }
                        }
                    }
                }
            }

            return Var_Bitmap;
        }

        /// <summary>
        /// 绘制带渐变的辉光文字
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="Font"></param>
        /// <param name="ForeColor"></param>
        /// <param name="BackColor"></param>
        /// <param name="BlurConsideration"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static Image BrushImageLightEffect(string txt, Font Font, Color ForeColor, Color BackColor, int BlurConsideration)
        {
            Bitmap Var_Bitmap = null;
            LinearGradientBrush brush;
            using (var g = Graphics.FromHwnd(IntPtr.Zero))
            {
                var size = g.MeasureString(txt, Font);
                using (var var_bmp = new Bitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height)))
                {
                    brush = new LinearGradientBrush(new Rectangle(0, 0, var_bmp.Width, var_bmp.Height), ForeColor, Color.FromArgb(150, ForeColor), LinearGradientMode.Horizontal);
                    using (var var_g_bmp = Graphics.FromImage(var_bmp))
                    {
                        using (var var_BrushBack = new SolidBrush(Color.FromArgb(16, BackColor)))
                        {
                            using (var var_ForeColor = new SolidBrush(ForeColor))
                            {
                                var_g_bmp.SmoothingMode = SmoothingMode.HighQuality;
                                var_g_bmp.InterpolationMode = InterpolationMode.HighQualityBilinear;
                                var_g_bmp.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                                var_g_bmp.DrawString(txt, Font, var_BrushBack, 0f, 0f);
                                Var_Bitmap = new Bitmap(var_bmp.Width + BlurConsideration, var_bmp.Height + BlurConsideration);
                                using (var var_g_bitmap = Graphics.FromImage(Var_Bitmap))
                                {
                                    var_g_bitmap.SmoothingMode = SmoothingMode.HighQuality;
                                    var_g_bitmap.InterpolationMode = InterpolationMode.HighQualityBilinear;
                                    var_g_bitmap.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                                    for (int x = 0, loopTo = BlurConsideration; x <= loopTo; x++)
                                    {
                                        for (int y = 0, loopTo1 = BlurConsideration; y <= loopTo1; y++)
                                            var_g_bitmap.DrawImageUnscaled(var_bmp, x, y);
                                    }

                                    var_g_bitmap.DrawString(txt, Font, brush, (float)(BlurConsideration / 2d), (float)(BlurConsideration / 2d));
                                }
                            }
                        }
                    }
                }
            }

            return Var_Bitmap;
        }
    }
}